package eu.winterwheels.enterprise.controller.supply;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import eu.winterwheels.enterprise.general.InternalStateMessage;
import eu.winterwheels.enterprise.service.iso.CountryService;

/**
 * This class models the complete supplier registration process. It's navigation
 * controller within the MVC pattern.
 * 
 * @author Brayan
 * 
 */
@ManagedBean
@SessionScoped
public class SupplierRegistrationNavigationController {

	/** List of the steps within the supplier registration process */
	private List<String> processStepList = new ArrayList<String>();
	private String currentProcessStep = null;

	private final String COUNTRY = "countryForm", ORGANIZATION = "organisationForm",
			DELIVERY_AREA = "deliveryAreaForm", FINANCIAL_TRANSACTION = "financialTransactionForm",
			COMMITMENT = "commitmentForm", VERIFICATION = "verificationForm";

//	@Inject
//	SupplierRegistrationController supRegController;
	
	/**
	 * Set up the CC-process list
	 */
	@PostConstruct
	private void init() {
		currentProcessStep = COUNTRY;
		processStepList.add(COUNTRY);
		processStepList.add(ORGANIZATION);
		processStepList.add(DELIVERY_AREA);
		processStepList.add(FINANCIAL_TRANSACTION);
		processStepList.add(COMMITMENT);
		processStepList.add(VERIFICATION);
	}

	/**
	 * Move to the next process step.
	 */
	public void next() {
		int i;
		for (i = processStepList.size() - 1; i > -1; i--) {
			if (processStepList.get(i).equals(currentProcessStep))
				break;
		}
		currentProcessStep = processStepList.get(i + 1);
	}

	/**
	 * Move to the previous step.
	 */
	public void previous() {
		int i;
		for (i = processStepList.size() - 1; i > -1; i--) {
			if (processStepList.get(i).equals(currentProcessStep))
				break;
		}
		currentProcessStep = processStepList.get(i - 1);
	}

	/**
	 * Moves to the process step. 
	 * @param processStep
	 *            The process step denoted by a string.
	 */
	public void moveToIndex(String processStep) {
		int i;
		for (i = processStepList.size() - 1; i > -1; i--) {
			if (processStepList.get(i).equals(processStep))
				break;
		}
		currentProcessStep = processStepList.get(i);
	}

	public boolean isToDisplay(String processStep) {
		return processStep.equalsIgnoreCase(currentProcessStep);
	}

	public boolean isToDisplayNextButton() {
		return !processStepList.get(processStepList.size() - 1).equals(currentProcessStep);
	}

	public boolean isToDisplayPrevButton() {
		return !processStepList.get(0).equals(currentProcessStep);
	}
	
	public CountryService getCountry(){
		return null; //supRegController.getCountryLogic();
	}

	/**
	 *TODO: MOVE SAVE METHOD TO SupplierRegistrationController?
	 * Saving the supplier registration details after the validation is
	 * successfull.
	 * 
	 * @return InternalStateMessage The health status must return true, if the
	 *         save procedure was successful.
	 */
	public String save() {

		// Validate if all required data is available. This will return an
		InternalStateMessage state = validateProvidedDataForCompleteness();

		// If available, send to EJB for persistence
		
		// Populate FacesMessages
		FacesMessage msg = new FacesMessage(state.getMessage());
		FacesContext.getCurrentInstance().addMessage(null, msg);
		// TODO is return message needed in a controller?!?!!
		// EJB --> InternalStateMessage --> Controller --> FacesMessage

		// Dispatch to the resumé page.
		return "donepage";
	}

	public InternalStateMessage validateProvidedDataForCompleteness() {
		return new InternalStateMessage(false, "Save method not yet implemented.");
	}		
}
